package com.bishe.controller;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bishe.entity.User;
import com.bishe.entity.prop.WxMaProperties;
import com.bishe.entity.resp.LoginInfo;
import com.bishe.entity.resp.R;
import com.bishe.entity.resp.UserInfo;
import com.bishe.exception.CommonException;
import com.bishe.service.UserService;
import io.swagger.annotations.Api;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/wx")
@Api(tags = "登录")
public class LoginController {

    @Resource
    private WxMaService wxMaService;
    @Resource
    private UserService userService;

    @Autowired
    WxMaProperties properties;

    @PostMapping("/login")
    public R<?> login(@RequestBody LoginInfo loginInfo) {
        UserInfo userInfo = userService.login(loginInfo);
        if (userInfo != null) {
            return R.data(userInfo);
        }
        return R.error("用户名密码错误!");
    }


/*    @PostMapping("/wxLogin")
    public R<?> wxLogin(@Valid @RequestBody LoginInfo loginInfo){
        //小程序登录参数Get请求
        String appid = properties.getConfigs().get(0).getAppid();
        WxMaJscode2SessionResult wxSession = getWxSession(appid, loginInfo.getCode());
        String openid = wxSession.getOpenid();
        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getOpneid, openid));
        if (user ==null){
            // 新增
            user = new User();
            user.setNumber(loginInfo.getNumber());
            user.setUsername(loginInfo.getUserName());
            user.setOpneid(openid);
            user.setType(loginInfo.getType());
            userService.save(user);
        }
        StpUtil.login(user.getId());
        StpUtil.getTokenSession().set("user", loginInfo);

        UserInfo userInfo = new UserInfo();
        userInfo.setName(loginInfo.getUserName());
        userInfo.setType(loginInfo.getType());
        userInfo.setToken(StpUtil.getTokenValue());
        return R.data(userInfo);
    }*/

    private WxMaJscode2SessionResult getWxSession(String appid, String code) {
        if (!wxMaService.switchover(appid)) {
            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置，请核实！", appid));
        }

        try {
            return wxMaService.getUserService().getSessionInfo(code);
        } catch (WxErrorException e) {
            throw new CommonException("微信登录异常");
        }
    }

}
